{extend name="public/base" /}{block name="title"}<title>{$Think.lang.list}</title>{/block}{block name="pjax_container"}
<div class="hry-content-wrapper">
<section class="content-header">
    <ol class="breadcrumb">
        <li>合同管理</li>
        <li class="active">合同重发</li>
    </ol>
</section>
<section class="content">
    <div class="row">
        <div class="col-md-12">
            <div class="hry-form-wrapper">
                <div class="box-header with-border"><h3 class="box-title"></h3>
                    <div class="pull-right"> {include file="public/hetongcf_search" /}
                        <button type="button" class="btn btn-primary add" data-toggle="modal" data-target="#myModal">新增</button></div>
                </div>
                <div class="box-body table-responsive">
                    <table class="table table-bordered table-hover table-sort">
                        <tr>
                            <th>{$Think.lang.id}{:table_sort('id')}</th>
                            <th>合同编号</th>
                            <th>用户名</th>
                            <th>身份证号</th>
                            <th>金额</th>
                            <th>合同名称</th>
                            <th>签署状态</th>
                            <th>新建时间</th>
                            <!--<th>发送状态</th>-->
                            <th width="178">{$Think.lang.action}</th>
                        </tr>
                        {volist name="dataList" id="vo"}
                        <tr>
                            <td>{$vo.id}</td>
                            <td>{$vo.contract_id}</td>
                            <td>{$vo.Member.username}</td>
                            <td>{$vo.certificate}</td>
                            <td>{$vo.money}</td>
                            <td>{$vo.title}</td>
                            <td> {if $vo.sign eq 3}签署成功{elseif $vo.sign eq 1}公司未签章{elseif $vo.sign eq 2}用户未签署{elseif $vo.sign eq 4}用户未签署{/if}</td>
                            <td>{$vo.create_time}</td>
                            <td><a class="btn-text btn-primary btn-xs" href="{:url('Hetong/detail',['id'=>$vo.id])}"
                                   target="_blank">查看</a>
                                {if $vo.sign eq 2}
                                <button type="submit" class="btn-text btn-primary btn-xs userzc" data-id="{$vo.user_id}">签署</button>
                                {/if}
                                {if $vo.issend eq 1}
                                <button type="submit" class="btn-text btn-primary btn-xs btnSubmit" data-id="{$vo.contract_id}">公司签章</button>
                                {elseif $vo.issend eq 2 }
                                <button type="submit" class="btn-text btn-primary btn-xs userbf" data-id="{$vo.contract_id}">合同补发</button>
                                {/if}
                            </td>
                        </tr>
                        {/volist}
                    </table>
                </div>
                <div class="box-footer clearfix"> {$dataList->render()}</div>
            </div>
        </div>
    </div>
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">合同重发</h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal">
                        <div class="form-group">
                            <label for="name" class="col-sm-2 control-label">用户姓名</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="name" maxlength="20" placeholder="请输入用户名">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="idcards" class="col-sm-2 control-label">身份证号</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="idcards" maxlength="18" placeholder="请输入身份证号">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="money" class="col-sm-2 control-label">小写金额</label>
                            <div class="col-sm-10">
                                <input type="number" class="form-control" id="money"  placeholder="请输入小写金额">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="moneyD" class="col-sm-2 control-label">大写金额</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="moneyD" disabled placeholder="请输入大写金额">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="c_id" class="col-sm-2 control-label">关联合同</label>
                            <div class="col-sm-10">

                                <select class="form-control" id="c_id" placeholder="请输入关联合同">
                                    {volist name="conlist" id="vo"}
                                    <option value="{$vo.id}">{$vo.title}</option>
                                    {/volist}
                                </select>
                            </div>
                        </div>
                        <div style="text-align: right">
                            <button type="button" id="addContract" class="btn btn-primary">提 交</button>
                            <button type="reset" class="btn btn-default">重 置</button>
                            <button type="button" class="btn btn-default" data-dismiss="modal">关 闭</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</section>
</div>
<script type="text/javascript">
    $(function () {    /*ajax页面加载icheck，有该控件的页面才需要*/
        $('input[type="checkbox"].minimal, input[type="radio"].minimal').iCheck({
            checkboxClass: 'icheckbox_minimal-blue',
            radioClass: 'iradio_minimal-blue'
        });




        $('.userzc').click(function (e) {
            var user_id = $(this).data('id');
            console.log(user_id);
            $.ajax({
                url: 'userzc',
                data: {user_id: user_id},
                type: "POST",
                dataType: "json",
                success: function (result) {
                    if (result.status == 1) {
                        $.amaran({'message': '生成成功!'});
                        console.log(result);
                        // return false;
                        window.location.href = result.url;
                    } else {
                        $.amaran({'message': '生成失败!'});
                    }
                },
                error: function (result, xhr, status, errorThrown) {
                    $.amaran({'message': result.info})
                },
                complete: function (xhr, status) {
//                    alert('The request is complated');
                },
                beforesend: function () {
                },
                cache: false,//true
                async: true
            })
        });

        $('.userbq').click(function (e) {
            var cid = $(this).data('id');
            var user_id=$(this).data('uid');
            $.ajax({
                url: 'userbq',
                data: {cid: cid,user_id:user_id},
                type: "POST",
                dataType: "json",
                success: function (result) {
                    if (result.status == 1) {
                        $.amaran({'message': '补签成功!'});
                        console.log(result);
                        window.location.href = result.url;
                    }  else {
                        $.amaran({'message': '补签失败!'});
                    }
                },
                error: function (result, xhr, status, errorThrown) {
                    $.amaran({'message': result.info})
                },
                complete: function (xhr, status) {
//                    alert('The request is complated');
                },
                beforesend: function () {
                },
                cache: false,//true
                async: true
            })
        });

        $('.btnSubmit').click(function (e) {
            var contract_id = $(this).data('id');
            console.log(contract_id);
            $.ajax({
                url: 'comcre',
                data: {contract_id: contract_id},
                type: "POST",
                dataType: "json",
                success: function (result) {
                    if (result.status == 1) {
                        $.amaran({'message': '盖章成功!'});
                        console.log(result);
                        window.location.href = result.url;
                    }  else {
                        $.amaran({'message': '盖章失败!'});
                    }
                },
                error: function (result, xhr, status, errorThrown) {
                    $.amaran({'message': result.info})
                },
                complete: function (xhr, status) {
//                    alert('The request is complated');
                },
                beforesend: function () {
                },
                cache: false,//true
                async: true
            })
        });

        $('.userbf').click(function (e) {
            var contract_id = $(this).data('id');
            console.log(contract_id);
            $.ajax({
                url: 'userbf',
                data: {contract_id: contract_id},
                type: "POST",
                dataType: "json",
                success: function (result) {
                    if (result.status == 1) {
                        $.amaran({'message': '补发成功!'});
                        console.log(result);
                        window.location.href = result.url;
                    } else {
                        $.amaran({'message': '补发失败!'});
                    }
                },
                error: function (result, xhr, status, errorThrown) {
                    $.amaran({'message': result.info})
                },
                complete: function (xhr, status) {
//                    alert('The request is complated');
                },
                beforesend: function () {
                },
                cache: false,//true
                async: true
            })
        });


        /*ajax页面加载icheck，有该控件的页面才需要*/
        $(".select2").select2({language: "zh-CN"});
        /*全选-反选*/
        $('.checkbox-toggle').on('ifChecked', function (event) {
            var _this = $(this);
            var _table = _this.closest('.table');
            _table.find("tr td input[type='checkbox']").iCheck("check");
        });
        $('.checkbox-toggle').on('ifUnchecked', function (event) {
            var _this = $(this);
            var _table = _this.closest('.table');
            _table.find("tr td input[type='checkbox']").iCheck("uncheck");
        });

        $('.editable').editable({
            emptytext: "empty",
            params: function (params) {
            }
        });

        $("#money").bind('input propertychange', function(){
            $this=$(this);
            let text = smalltoBIG($this.val())
            $("#moneyD").val(text)
        })
        // 增加合同
        $('#addContract').click(function (e) {
            var name = $("#name").val();
            var idcards = $("#idcards").val();
            var money = $("#money").val();
            var c_id = $("#c_id").val();

            if(name=== '' || idcards=== '' || money === '' || c_id=== '') {
                $.amaran({'message': '请将资料填写完整!'});
                return false
            }
            // 匹配汉字
//            let reg=/^[\u2E80-\u9FFF]+$/;
            let reg=/^[\u4E00-\u9FA5\uf900-\ufa2d·s]{2,20}$/; // 验证包含少数名族的名字
            if(!reg.test(name)){
                $.amaran({'message': '姓名为中文字符!'});
                return false
            }
            // 匹配身份证
            var idreg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
            if(!idreg.test(idcards)){
                $.amaran({'message': '请输入正确的身份证号码!'});
                return false
            }
            var numberReg = /^[0-9]*$/;
            if(!numberReg.test(money)){
                $.amaran({'message': '请输入金额为大于零的整数!'});
                return false
            } else if (money === 0) {
                $.amaran({'message': '金额不为零!'});
                return false
            } else if (money.indexOf(0) === 0){
                $.amaran({'message': '请输入金额首位数字不为零!'});
                return false
            }
            let obj ={
                name: name,
                idcards: idcards,
                money: money,
                c_id: c_id
            }
            $.ajax({
                url: 'hetongretry',
                data: obj,
                type: "POST",
                dataType: "json",
                success: function (result) {
                    if (result.status == 1) {
                        $.amaran({'message': result.info});
                        console.log(result);
                        window.location.href = result.url;
                    } else {
                        $.amaran({'message': result.info});
                    }
                },
                error: function (result, xhr, status, errorThrown) {
                    $.amaran({'message': result.info})
                },
                complete: function (xhr, status) {
//                    alert('The request is complated');
                },
                beforesend: function () {
                },
                cache: false,//true
                async: true
            })
        });

    });
    // 金额转换
    function smalltoBIG(n)
    {
        var fraction = ['角', '分'];
        var digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
        var unit = [ ['元', '万', '亿'], ['', '拾', '佰', '仟']  ];
        var head = n < 0? '欠': '';
        n = Math.abs(n);

        var s = '';

        for (var i = 0; i < fraction.length; i++)
        {
            s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '');
        }
        s = s || '整';
        n = Math.floor(n);

        for (var i = 0; i < unit[0].length && n > 0; i++)
        {
            var p = '';
            for (var j = 0; j < unit[1].length && n > 0; j++)
            {
                p = digit[n % 10] + unit[1][j] + p;
                n = Math.floor(n / 10);
            }
            s = p.replace(/(零.)*零$/, '').replace(/^$/, '零')  + unit[0][i] + s;
        }
        return head + s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整');
    }
</script>
{/block}